﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ProcelNasEscolas.Data;
using ProcelNasEscolas.Business;
using System.Web.Services.Protocols;
using System.Web.Security;

namespace ProcelNasEscolas.WebServiceTeste
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public AlunoData[] getAluno()
        {
                return new AlunoDao().getAluno();   
        }

        private string AuthenticateUser(string strUser, string strPwd)
        {
            if (strUser == "silan" && strPwd == "silan")
                return "silan's role";
            else if (strUser == "yang" && strPwd == "yang")
                return "yang's role";
            else
                return null;
        }
        [WebMethod]
        public bool Login(string strUser, string strPwd)
        {
            string strRole = AuthenticateUser(strUser, strPwd);

            if (strRole != null)
            {
                // Issue the authentication key to the client
                FormsAuthentication.SetAuthCookie(strUser, false);
                return true;
            }
            else
                return false;
        }

        [WebMethod]
        public void LogOut()
        {
            // Deprive client of the authentication key
            FormsAuthentication.SignOut();
        }

        [WebMethod]
        public string GetMsg()
        {
            string s = "Authenticated: " + Context.User.Identity.IsAuthenticated.ToString() +
                "\nFormsCookieName: " + FormsAuthentication.FormsCookieName +
                "\nUser name: " + Context.User.Identity.Name +
                "\nIs in role \"silan's role\": " + Context.User.IsInRole("silan's role").ToString() +
                "\nIs in role \"yang's role\": " + Context.User.IsInRole("yang's role").ToString();

            FormsAuthentication.SignOut();
            return s;
        }
    }
}